package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.domain.WIncomerentRecord;
import cn.chuanw.db.domain.WRoomInfo;
import cn.chuanw.db.domain.WTenantInfo;
import cn.chuanw.db.service.IncomeRentRecordService;
import cn.chuanw.db.service.RoomService;
import cn.chuanw.db.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Auther: wangkun
 * @Date: 20-02-09 下午 4:06
 * @Description: 收租记录
 **/
@RestController
@RequestMapping("/admin/incomeRentRecordRecord")
@Validated
public class WIncomerentRecordController {
    private final Log logger = LogFactory.getLog(WIncomerentRecordController.class);

    @Autowired
    private IncomeRentRecordService incomeRentRecordService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private RoomService roomService;

    @RequiresPermissions("admin:payRentRecord:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "收租记录"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "tenantId", required = false) String tenantId,
                       @RequestParam(value = "houseId", required = false) String houseId,
                       @RequestParam(value = "roomId", required = false) String roomId,
                       @RequestParam(value = "payDate", required = false) String payDate,
                       @RequestParam(value = "userId", required = false) String userId,
                       @RequestParam(value = "incomeStatus", required = false) Integer incomeStatus,
                       @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = null;
        if (!StringUtils.isEmpty(payDate)) {
            localDate = LocalDate.parse(payDate, formatter);
        }
        List<WIncomerentRecord> wIncomerentRecords = incomeRentRecordService.querySelective(tenantId,houseId, roomId, localDate,
                userId, incomeStatus, page, limit);
        return ResponseUtil.okList(wIncomerentRecords);
    }

    @PostMapping("/createByTenantId")
    public Object createByTenantId(@RequestBody WIncomerentRecord wIncomerentRecord) {
        String tenantId = wIncomerentRecord.getTenantId();
        String userId = wIncomerentRecord.getUserId();
        Integer incomeAmount = wIncomerentRecord.getIncomeAmount();
        if (StringUtils.isEmpty(tenantId) && StringUtils.isEmpty(userId)) {
            return ResponseUtil.fail();
        }
        WTenantInfo wTenantInfo = tenantService.queryById(tenantId);
        WRoomInfo wRoomInfo = roomService.queryById(wTenantInfo.getRoomId());
        WIncomerentRecord wIncomerentRecordNew = new WIncomerentRecord();
        wIncomerentRecordNew.setRoomId(wTenantInfo.getRoomId());
        wIncomerentRecordNew.setUserId(userId);
        wIncomerentRecordNew.setTenantId(tenantId);
        wIncomerentRecordNew.setIncomeAmount(incomeAmount);
//		wIncomerentRecordNew.setIncomeAmount(wRoomInfo.getRoomRent());
        wIncomerentRecordNew.setIncomeStatus(1);
        wIncomerentRecordNew.setIncomeType(0);
        wIncomerentRecordNew.setIncomeTime(LocalDate.now());
        wIncomerentRecordNew.setId(RandomNumberUtil.getStringRandom(12));
        incomeRentRecordService.create(wIncomerentRecordNew);
        wTenantInfo.setPayrentStatus(1); // 修改为已收租状态
        //
        wTenantInfo.setPayDate(wTenantInfo.getPayDate().plusMonths(wTenantInfo.getRentCycle()));
        tenantService.updateById(wTenantInfo);
        return ResponseUtil.ok(wIncomerentRecordNew);
    }

    @RequiresPermissions("admaddin:payRentRecord:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "收租记录"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WIncomerentRecord wIncomerentRecord) {
        wIncomerentRecord.setIncomeTime(LocalDate.now());
        incomeRentRecordService.updateById(wIncomerentRecord);
        return ResponseUtil.ok(wIncomerentRecord);
    }


    @RequiresPermissions("admaddin:payRentRecord:archiveIncomeRent")
    @RequiresPermissionsDesc(menu = {"公寓管理", "收租记录"}, button = "归档")
    @PostMapping("/archiveIncomeRent")
    public Object archiveIncomeRent(@RequestParam(value = "ids", required = false) String ids) {
        incomeRentRecordService.archiveIncomeRent(ids);
        return ResponseUtil.ok();
    }

    /**
     * @Auther: wangkun
     * @Date: 20-03-24 上午 9:46
     * @Description: 计算当前收租金额，归档收租金额
     **/
    @GetMapping("/tatolIncomeRent")
    public Object tatolIncomeRent() {
        Object o = incomeRentRecordService.tatolIncomeRent();
        return ResponseUtil.ok(o);
    }
}
